$(document).ready(function() {

    queryWeather();
    queryAllNotice();
});

var queryWeather=function () {
    $.ajax({
        url: "https://free-api.heweather.com/s6/weather/forecast?key=2ed7a7d1ad454d958403beae02962538&location=tianjin",
        type: "GET",
        async: true,
        data: null,
        dataType: "json",
        success:function (data) {
            var loaction=data.HeWeather6[0].basic.parent_city;
            var dailyWeather=data.HeWeather6[0].daily_forecast[0].tmp_min+"℃~"+data.HeWeather6[0].daily_forecast[0].tmp_max+"℃";
            var one=data.HeWeather6[0].daily_forecast[0].date;
            var oneWeather=dailyWeather=data.HeWeather6[0].daily_forecast[0].tmp_min+"℃~"+data.HeWeather6[0].daily_forecast[0].tmp_max+"℃";

            var two=data.HeWeather6[0].daily_forecast[1].date;
            var twoWeather=dailyWeather=data.HeWeather6[0].daily_forecast[1].tmp_min+"℃~"+data.HeWeather6[0].daily_forecast[1].tmp_max+"℃";

            var three=data.HeWeather6[0].daily_forecast[2].date;
            var threeWeather=dailyWeather=data.HeWeather6[0].daily_forecast[2].tmp_min+"℃~"+data.HeWeather6[0].daily_forecast[2].tmp_max+"℃";

            $('#location').text(dailyWeather+" "+loaction);
            $('#one').text(one);
            $('#oneWeather').text(oneWeather);
            $('#two').text(two);
            $('#twoWeather').text(twoWeather);
            $('#three').text(three);
            $('#threeWeather').text(threeWeather);

        },
        error:function () {
            layer.msg("天气更新失败！请检查网络链接！");
        }
    });
};


var queryAllNotice=function(){
    $.ajax({
        url: "http://localhost:8080/notice/queryNotice",
        type: "GET",
        async: true,
        data: null,
        dataType: "json",
        success:function (data) {
            var list="";
            $.each(data.data.noticeList,function (index, value) {
                list+="<a name='aNotice' href='javascript:void(0);' onclick='ajax(this.id)' id="+value.noticeId+" class='list-group-item'>"+"<span>"+value.noticeId+"</span>."+value.noticeTitle+"<span style='margin-left:5px '>"+value.noticeDatetime+"</span>"+"</a>";
            })
            $('#notice').append(list);
        },
        error:function () {
            layer.msg("公告拉取失败！请检查网络链接！");
        }
    })
}

function ajax(noticeId) {


    $.ajax({
        url:"http://localhost:8080/notice/queryANotice?noticeId="+noticeId,
        type: "GET",
        async: true,
        data: null,
        dataType: "json",
        success:function (data) {
            var noticeContent=data.data.noticeTeacher.noticeContent;
            var htmlContent=html_decode(noticeContent);
            layer.open({
                type: 1,
                title:'通知详情',
                skin: 'layui-layer-rim', //加上边框
                area: ['420px', '240px'], //宽高
                content: htmlContent
            });
        },
        error:function () {

        }
    })
}

function html_decode(str) {
    var s = "";
    if (str.length == 0)
        return "";
    s = str.replace(/&lt;/g, "<");
    s = s.replace(/&gt;/g, ">");
    s = s.replace(/&nbsp;/g, " ");
    s = s.replace(/&#39;/g, "\'");
    s = s.replace(/&quot;/g, "\"");
    s = s.replace(/<br>/g, "\n");
    return s;
}